package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.ListingToPayBeen;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeListBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitEditBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ListingToPayPresenter;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：我的清单 未支付列表
 */
public class ProcurementListingToPayAdapter extends RecyclerView.Adapter<ProcurementListingToPayAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ListingToPayBeen.DataBean.PurchaseListBean> list;
    private Context context;
    private ListingToPayPresenter mPresenter;
    private boolean is_has;


    public ProcurementListingToPayAdapter(Context context, List<ListingToPayBeen.DataBean.PurchaseListBean> list, ListingToPayPresenter mPresenter,boolean is_has) {
        this.list = list;
        this.context = context;
        this.mPresenter = mPresenter;
        this.is_has = is_has;


    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_procurement_listing_topay, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        ProcurementListingItemAdapter procurementTypeListAdapter = new ProcurementListingItemAdapter(context, list.get(position).getGoods_list(), this, position, list.get(position).isIs_update());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rvProcurementList.setLayoutManager(linearLayoutManager);
        holder.rvProcurementList.setItemAnimator(new DefaultItemAnimator());
        holder.rvProcurementList.setNestedScrollingEnabled(false);
        holder.rvProcurementList.setAdapter(procurementTypeListAdapter);

        holder.tvDate.setText("日期：" + list.get(position).getDate());
        holder.tvSelectTypeNum.setText(list.get(position).getCategory_num() + "");
        holder.tvSelectJianNum.setText(list.get(position).getGoods_num() + "");

        if (list.get(position).isIs_update()) {
            holder.tvUpdate.setBackgroundResource(R.drawable.db3b3b3_bg_r3);
            holder.tvSave.setBackgroundResource(R.drawable.d0ea9f9_bg_r3);
        } else {
            holder.tvSave.setBackgroundResource(R.drawable.db3b3b3_bg_r3);
            holder.tvUpdate.setBackgroundResource(R.drawable.d0ea9f9_bg_r3);
        }

        if (is_has){
            holder.tvUpdate.setVisibility(View.GONE);
            holder.tvSave.setVisibility(View.GONE);
        }else {
            holder.tvUpdate.setVisibility(View.VISIBLE);
            holder.tvSave.setVisibility(View.VISIBLE);
        }
        holder.tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).setIs_update(true);

                notifyDataSetChanged();
            }
        });
        holder.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isIs_update()) {
                    list.get(position).setIs_update(false);
                    PurchaseSubmitEditBeen purchaseSubmitEditBeen = new PurchaseSubmitEditBeen();
                    purchaseSubmitEditBeen.setPurchase_sn(list.get(position).getPurchase_sn());
                    for (PurchaseSubmitBeen.DataBean.GoodsListBean goodsListBean : list.get(position).getGoods_list()) {
                        PurchaseSubmitEditBeen.UpdateDataBeen updateDataBeen = new PurchaseSubmitEditBeen.UpdateDataBeen();
                        updateDataBeen.setId(goodsListBean.getId());
                        updateDataBeen.setGoods_max_price(goodsListBean.getGoods_max_price());
                        updateDataBeen.setGoods_min_price(goodsListBean.getGoods_min_price());
                        updateDataBeen.setGoods_name(goodsListBean.getGoods_name());
                        updateDataBeen.setGoods_num(goodsListBean.getGoods_num());
                        updateDataBeen.setPurchase_type(goodsListBean.getPurchase_type());
                        purchaseSubmitEditBeen.getUpdate_data().add(updateDataBeen);
                    }

                    mPresenter.purchaseEdit(purchaseSubmitEditBeen, position);
                }
            }
        });

        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(position);
            }
        });
    }


    @Override
    public int getItemCount() {

        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.rv_procurement_list)
        RecyclerView rvProcurementList;
        @BindView(R.id.tv_select_type_num)
        TextView tvSelectTypeNum;
        @BindView(R.id.tv_select_jian_num)
        TextView tvSelectJianNum;
        @BindView(R.id.tv_update)
        TextView tvUpdate;
        @BindView(R.id.tv_save)
        TextView tvSave;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
